import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/button/brn_normal_button.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/max_line_wrap.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/views/mine/maintain/maintain_view.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';

import 'upkeep_logic.dart';

class MineUpkeepView extends GetView<MineUpkeepLogic> {
  const MineUpkeepView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: 15,
            ),
            onPressed: Get.back,
          ),
          automaticallyImplyLeading: false,
          backgroundColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
          title: Text(
            '我的保养',
            style: StylesUtils.customTextStyle(
                fontSize: 18, fontWeight: FontWeight.w700),
          ),
          centerTitle: true,
        ),
        body: RefreshIndicator(
            onRefresh: () {
              return controller.onRefresh(context);
            },
            child: scrollView()));
  }

  Widget scrollView() {
    return Obx(() => ListView(
          padding: const EdgeInsets.all(10),
          controller: controller.scrollController,
          physics: const AlwaysScrollableScrollPhysics(),
          children: [
            StickyHeader(
              header: Container(
                height: 40,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(5),
                        topRight: Radius.circular(5))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: 80,
                      height: 40,
                      padding: const EdgeInsets.only(left: 15),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '统计',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color1c24,
                            fontWeight: FontWeight.w700),
                      ),
                    ),
                    Container(
                        height: 40,
                        padding: const EdgeInsets.only(right: 15),
                        alignment: Alignment.centerRight,
                        child: SizedBox(
                          height: 25,
                          child: Row(
                              children: controller.tabs
                                  .asMap()
                                  .map((index, value) => MapEntry(
                                      index,
                                      Obx(() => InkWell(
                                          onTap: () {
                                            controller.tabIndex.value = index;
                                          },
                                          child: Container(
                                            constraints: BoxConstraints.tight(
                                                const Size.fromWidth(60)),
                                            alignment: Alignment.center,
                                            decoration: BoxDecoration(
                                                color: index ==
                                                        controller
                                                            .tabIndex.value
                                                    ? ColorsUtils
                                                        .parseColorFromHexString(
                                                            ColorsUtils
                                                                .color0052)
                                                    : Colors.transparent,
                                                border: getBorder(
                                                    index, controller.tabs),
                                                borderRadius: getBorderRadius(
                                                    index, controller.tabs)),
                                            child: Text(
                                              value,
                                              style:
                                                  StylesUtils.customTextStyle(
                                                      color: index ==
                                                              controller
                                                                  .tabIndex
                                                                  .value
                                                          ? ColorsUtils
                                                              .colorFfff
                                                          : ColorsUtils
                                                              .color9999,
                                                      fontSize: 13),
                                            ),
                                          )))))
                                  .values
                                  .toList()),
                        ))
                  ],
                ),
              ),
              content: Container(
                  height: 100,
                  margin: const EdgeInsets.only(bottom: 10),
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5))),
                  child: Row(
                    children: [
                      Expanded(
                          child: Container(
                        padding: const EdgeInsets.only(top: 24),
                        child: Column(
                          children: [
                            Container(
                              height: 26,
                              alignment: Alignment.center,
                              child: Text(
                                '${controller.task.value}',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0052,
                                    fontWeight: FontWeight.w700,
                                    fontSize: 20),
                              ),
                            ),
                            Container(
                              height: 30,
                              alignment: Alignment.center,
                              child: Text(
                                '保养任务',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color9999),
                              ),
                            )
                          ],
                        ),
                      )),
                      Expanded(
                          child: CustomPaint(
                        painter: CustomPaintVertical(),
                        child: Container(
                          padding: const EdgeInsets.only(top: 24),
                          child: Column(
                            children: [
                              Container(
                                height: 26,
                                alignment: Alignment.center,
                                child: Text(
                                  '${controller.order.value}',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color0052,
                                      fontWeight: FontWeight.w700,
                                      fontSize: 20),
                                ),
                              ),
                              Container(
                                height: 30,
                                alignment: Alignment.center,
                                child: Text(
                                  '保养工单',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color9999),
                                ),
                              )
                            ],
                          ),
                        ),
                      ))
                    ],
                  )),
            ),
            StickyHeader(
                header: Container(
                    height: 40,
                    decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(5),
                            topRight: Radius.circular(5))),
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          width: 80,
                          height: 40,
                          padding: const EdgeInsets.only(left: 15),
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '保养记录',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color1c24,
                                fontWeight: FontWeight.w700),
                          ),
                        ),
                        Container(
                            height: 40,
                            padding: const EdgeInsets.only(right: 15),
                            alignment: Alignment.centerRight,
                            child: SizedBox(
                              height: 25,
                              child: Row(
                                  children: controller.taskTab
                                      .asMap()
                                      .map((index, value) => MapEntry(
                                          index,
                                          Obx(() => InkWell(
                                              onTap: () {
                                                controller.taskIndex.value =
                                                    index;
                                              },
                                              child: Container(
                                                constraints:
                                                    BoxConstraints.tight(
                                                        const Size.fromWidth(
                                                            60)),
                                                alignment: Alignment.center,
                                                decoration: BoxDecoration(
                                                    color: index ==
                                                            controller
                                                                .taskIndex.value
                                                        ? ColorsUtils
                                                            .parseColorFromHexString(
                                                                ColorsUtils
                                                                    .color0052)
                                                        : Colors.transparent,
                                                    border: getBorder(index,
                                                        controller.taskTab),
                                                    borderRadius:
                                                        getBorderRadius(
                                                            index,
                                                            controller
                                                                .taskTab)),
                                                child: Text(
                                                  value,
                                                  style: StylesUtils
                                                      .customTextStyle(
                                                          color: index ==
                                                                  controller
                                                                      .taskIndex
                                                                      .value
                                                              ? ColorsUtils
                                                                  .colorFfff
                                                              : ColorsUtils
                                                                  .color9999,
                                                          fontSize: 13),
                                                ),
                                              )))))
                                      .values
                                      .toList()),
                            ))
                      ],
                    )),
                content: Container(
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5))),
                  child: Column(
                    children: [
                      ...controller.pageList
                          .map((e) => DrawUnderLine(
                              color: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color6666),
                              child: Container(
                                decoration:
                                    const BoxDecoration(color: Colors.white),
                                padding:
                                    const EdgeInsets.symmetric(horizontal: 15),
                                child: ListBody(
                                  children: [
                                    Container(
                                      height: 40,
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        e.title,
                                        style: StylesUtils.customTextStyle(
                                            color: ColorsUtils.color0052,
                                            fontSize: 16,
                                            fontWeight: FontWeight.w700),
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '任务单号',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.taskNo,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '设备名称',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.name,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '设备编号',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.code,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '存放位置',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.location,
                                            overflow: TextOverflow.ellipsis,
                                            maxLines: 1,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '紧急程度',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            '${e.degree?.label}',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 40,
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            e.addTime,
                                            style: StylesUtils.customTextStyle(
                                                fontSize: 13,
                                                color: ColorsUtils.color3333),
                                          ),
                                          BrnNormalButton(
                                            onTap: () {},
                                            insertPadding:
                                                const EdgeInsets.only(top: 3),
                                            constraints:
                                                const BoxConstraints.expand(
                                                    width: 60, height: 25),
                                            text: e.task?.label ?? '',
                                            textStyle:
                                                StylesUtils.customTextStyle(
                                                    fontSize: 12,
                                                    color: '${e.task?.color}'),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                              color: ColorsUtils
                                                  .parseColorFromHexString(
                                                      '${e.task?.color}'),
                                            )),
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              )))
                          .toList(),
                      renderBottom(),
                    ],
                  ),
                ))
          ],
        ));
  }

  BorderRadius? getBorderRadius(int index, List<String> tabs) {
    if (index == 0) {
      return const BorderRadius.only(
        topLeft: Radius.circular(5),
        bottomLeft: Radius.circular(5),
      );
    }
    if (index == tabs.length - 1) {
      return const BorderRadius.only(
          topRight: Radius.circular(5), bottomRight: Radius.circular(5));
    }
    return null;
  }

  Border getBorder(int index, List<String> tabs) {
    if (index == 0 || index + 1 == tabs.length) {
      return Border.all(
          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9));
    }
    if (index == tabs.length - 2) {
      return Border(
        top: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        bottom: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
      );
    }
    return Border(
        top: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        bottom: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        right: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)));
  }

  Widget renderBottom() {
    if (controller.isLoading.value) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: const Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '努力加载中...',
              style: TextStyle(
                fontSize: 15,
                color: Color(0xFF333333),
              ),
            ),
            Padding(padding: EdgeInsets.only(left: 10)),
            SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(strokeWidth: 3),
            ),
          ],
        ),
      );
    }
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 15),
      alignment: Alignment.center,
      child: const Text(
        '上拉加载更多',
        style: TextStyle(
          fontSize: 15,
          color: Color(0xFF333333),
        ),
      ),
    );
  }
}
